package com.homedo.odin.solution.controller.pc;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.pc.PcColumnListRequestBean;
import com.homedo.odin.solution.contract.pc.PcSpecialColumnPageResponseBean;
import com.homedo.odin.solution.contract.pc.SpecialColumnPcDetailResponseBean;
import com.homedo.odin.solution.contract.pc.SpecialColumnResponseBean;
import com.homedo.odin.solution.enumcode.column.ColumnTypeEnum;
import com.homedo.odin.solution.model.request.pc.brand.PcBrandColumnListRequestBean;
import com.homedo.odin.solution.model.request.pc.column.ColumnDetailRequestBean;
import com.homedo.odin.solution.model.response.pc.column.BrandColumnListResponseBean;
import com.homedo.odin.solution.model.response.pc.column.SpecialColumnFiveListResponseBean;
import com.homedo.odin.solution.service.SpecialColumnServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zhanglei
 * @date: 2018/4/9
 * @Description:
 */
@Api(description = "栏目", tags = {"Column"},
        produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping(("/special_columns"))
public class SpecialColumnController {

    @Autowired
    private SpecialColumnServiceImpl specialColumnService;

    @GetMapping
    @ApiOperation(value = "PC_分页获取栏目信息列表", httpMethod = "GET", response = SpecialColumnResponseBean.class, notes = "根据标签名称获取栏目数据，根据品牌主获取栏目数据，根据栏目类型获取栏目数据，3项都不传只传分页参数则获取热门资讯智慧视野")
    public ResultData querySpecialColumnListByParams(@Valid @ModelAttribute PcColumnListRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        PcSpecialColumnPageResponseBean responseBean = specialColumnService.selectSpecialColumnList(requestBean);
        return ResultData.newInstance(Meta.CODE_200, responseBean);
    }


    @GetMapping("/{columnId}")
    @ApiOperation(value = "PC_获取栏目详细信息", httpMethod = "GET", response = SpecialColumnPcDetailResponseBean.class)
    public ResultData querySpecialColumnDetail(@ApiParam(value = "栏目id") @PathVariable("columnId") Long columnId,
            @ApiParam(value = "当前登录用户id") @RequestParam(required = false) Integer userId) {
        if (!NumberHelper.validate(columnId)) {
            throw new BizException(Meta.CODE_451.getCode(), "columnId参数错误");
        }
        SpecialColumnPcDetailResponseBean responseBean = specialColumnService
                .querySpecialColumnDetail(columnId, userId);
        return ResultData.newInstance(Meta.CODE_200, responseBean);
    }


    @PostMapping("/likes")
    @ApiOperation(value = "PC_点赞栏目", httpMethod = "POST")
    public ResultData likeSpecialColumn(@RequestBody ColumnDetailRequestBean requestBean, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        specialColumnService.likeSpecialColumn(requestBean.getColumnId());
        return ResultData.newInstance(Meta.CODE_200, null);
    }


    @GetMapping("/fiveList")
    @ApiOperation(value = "获取当前前五个往期栏目信息", response = SpecialColumnFiveListResponseBean.class, httpMethod = "GET", notes = "获取已发布的往期栏目")
    public ResultData querySpecialColumnFiveList(
            @ApiParam(value = "当前栏目id", required = true) @RequestParam Long columnId,
            @ApiParam(value = "栏目类型（1.专栏栏目2.视频栏目）", required = true) @RequestParam Integer columnType) {
        if (!ColumnTypeEnum.dbValueExist(columnType)) {
            throw new BizException(Meta.CODE_451.getCode(), "栏目类型参数错误  （1.专栏栏目2.视频栏目）");
        }

        List<SpecialColumnFiveListResponseBean> list = specialColumnService
                .querySpecialColumnFiveList(columnId, columnType);
        return ResultData.newInstance(Meta.CODE_200, list);
    }

    @GetMapping("/brand_firm/{uid}")
    @ApiOperation(value = "PC_获取品牌主的栏目列表", response = BrandColumnListResponseBean.class, httpMethod = "GET")
    public ResultData getBrandFirmColumnsByUser(@PathVariable("uid") Integer uid,
            @Valid @ModelAttribute PcBrandColumnListRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultData.newInstance(new Meta(Meta.CODE_451.getCode(),
                    bindingResult.getFieldError().getDefaultMessage()));
        }
        if (!NumberHelper.validatePositive(uid)) {
            throw new BizException(Meta.CODE_451.getCode(), "用户 ID 参数非法");
        }
        return ResultData.newInstance(specialColumnService.getColumnsByUser(uid, requestBean));
    }


}
